package com.www.applyword.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

public class ListDataSave {
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    private List<String> mDatalist;

    private static final String NOTICE_DATA = "notice_data";
    private static final String NOTICE_DATA2 = "notice_data2";

    private static final String NOTICE_DATA_ITEM = "notice_data_item";

    private static ListDataSave listDataSave;

    private ListDataSave(Context mContext) {
        preferences = mContext.getSharedPreferences(NOTICE_DATA, Context.MODE_PRIVATE);
        editor = preferences.edit();
    }


    public static ListDataSave getInstence(Context mContext){
        if (listDataSave == null){
            listDataSave = new ListDataSave(mContext);
        }
        return listDataSave;
    }

    /**
     * 保存List
     * @param datalist
     */
    public void setDataList(List<String> datalist) {
        if (null == datalist || datalist.size() <= 0)
            //  return;
            mDatalist = datalist;
        Gson gson = new Gson();
        //转换成json数据，再保存
        //  Collections.reverse(datalist);
        String strJson = gson.toJson(datalist);
        editor.clear();
        editor.putString(NOTICE_DATA, strJson);
        editor.apply();
    }


    /**
     * 保存List
     * @param datalist
     */
    public void setData(String datalist) {
        if (null == datalist)
            return;
        Gson gson = new Gson();
        //转换成json数据，再保存
        String strJson = gson.toJson(datalist);
        editor.clear();
        editor.putString(NOTICE_DATA_ITEM, strJson);
        editor.apply();
    }

    /**
     * 获取List
     * @param
     * @return
     */
    public  String getData() {
        String strJson = preferences.getString(NOTICE_DATA_ITEM, null);
        if (null == strJson) {
            return null;
        }
        Gson gson = new Gson();
        String datalist = gson.fromJson(strJson, new TypeToken<String>() {}.getType());
        return datalist;
    }


    /**
     * 添加List
     * @param notifyData
     */
    public void addDataList(String notifyData) {
        // Log.i("wyb","addDataList is " + mDatalist.size());
        //  mDatalist.add(notifyData);
        mDatalist = getDataList();
        if (null == mDatalist) {
            mDatalist = new ArrayList<>();
            mDatalist.add(notifyData);
        }else {
            mDatalist.add(notifyData);
        }

        for (int i = 0; i < mDatalist.size() - 1; i++) {
            for (int j = mDatalist.size() - 1; j > i; j--) {
                if (mDatalist.get(j).equals(mDatalist.get(i))) {
                    //删除重复元素
                    mDatalist.remove(j);
                }
            }
        }

        //  Set<NotifyData> studentSet = new TreeSet<>((o1, o2) -> o1.getTime().compareTo(o2.getTime()));
        //  studentSet.addAll(mDatalist);

        setDataList((mDatalist));
    }



    /**
     * 添加List
     * @param notifyData
     */
    public void addExpressionCreatTimeList(String notifyData) {
        // Log.i("wyb","addDataList is " + mDatalist.size());
        //  mDatalist.add(notifyData);
        mDatalist = getDataList();
        if (null == mDatalist) {
            mDatalist = new ArrayList<>();
            mDatalist.add(notifyData);
        }else {
            mDatalist.add(notifyData);
        }

        //  Set<NotifyData> studentSet = new TreeSet<>((o1, o2) -> o1.getTime().compareTo(o2.getTime()));
        //  studentSet.addAll(mDatalist);

        // setExpCreatTimeDataList((mDatalist));
    }

    public void setExpCreatTimeDataList(List<String> datalist) {
        Gson gson = new Gson();
        //转换成json数据，再保存
        //  Collections.reverse(datalist);
        String strJson = gson.toJson(datalist);
        editor.clear();
        editor.putString(NOTICE_DATA2, strJson);
        editor.apply();
    }

    /**
     * 更新List
     * @param notifyData
     */
    public void setListData(int i,String notifyData) {
        Log.i("wyb","addDataList is " + mDatalist.size());
        mDatalist.set(i,notifyData);
        setDataList(mDatalist);
    }

    /**
     * 删除List
     * @param
     */
    public void removeListData(int i) {
        Log.i("wyb","addDataList is " + mDatalist.size());
        mDatalist.remove(i);
        // mDatalist.remove(notifyData);
        setDataList(mDatalist);
    }


    /**
     * 获取List
     * @param
     * @return
     */
    public  List<String> getDataList() {
        List<String> datalist = new ArrayList<>();
        String strJson = preferences.getString(NOTICE_DATA, null);
        if (null == strJson) {
            return datalist;
        }
        Gson gson = new Gson();
        datalist = gson.fromJson(strJson, new TypeToken<List<String>>() {
        }.getType());
        return datalist;
    }

    /**
     * 获取List
     * @param
     * @return
     */
    public  List<String> getDataList2() {
        List<String> datalist = new ArrayList<>();
        String strJson = preferences.getString(NOTICE_DATA2, null);
        if (null == strJson) {
            return datalist;
        }
        Gson gson = new Gson();
        datalist = gson.fromJson(strJson, new TypeToken<List<String>>() {
        }.getType());
        return datalist;
    }

    public void removeDificult() {//删除某个首选项
        editor.remove(NOTICE_DATA).commit();
    }
}
